package com.select.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.select.course.entity.SchoolClass;
import com.select.course.entity.SchoolCollege;
import com.select.course.entity.SchoolMajor;
import com.select.course.mapper.SchoolClassMapper;
import com.select.course.service.SchoolClassService;
import com.select.course.service.SchoolCollegeService;
import com.select.course.service.SchoolMajorService;
import com.select.course.service.UserStudentService;
import com.select.course.utils.PageUtils;
import com.select.course.utils.R;
import com.select.course.vo.ClassVo;
import com.select.course.vo.PageVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 24820
* @description 针对表【c_school_class】的数据库操作Service实现
* @createDate 2022-10-26 23:08:18
*/
@Service
public class SchoolClassServiceImpl extends ServiceImpl<SchoolClassMapper, SchoolClass>
    implements SchoolClassService{
    @Resource
    private SchoolMajorService schoolMajorService;
    @Resource
    private SchoolCollegeService schoolCollegeService;
    @Lazy
    @Resource
    private UserStudentService userStudentService;

    @Override
    public List<SchoolClass> getClassNameGroup() {
        List<SchoolClass> list = this.list();

        return list;
    }
    @Override
    public List<ClassVo> getClassByMajorName(String name) {
        SchoolMajor name1 = schoolMajorService.getOne(new QueryWrapper<SchoolMajor>().eq("name", name));
        List<SchoolClass> schoolClasses = this.list(new QueryWrapper<SchoolClass>().eq("major_id", name1.getId()));
        List<ClassVo> collect = schoolClasses.stream().map(e -> {
            ClassVo classVo = new ClassVo();
            classVo.setClassName(e.getName());
            Map<String, String> stringStringMap = userStudentService.packageCollegeAndMajor(e.getCollegeId(), e.getMajorId(), null);
            classVo.setMajor(stringStringMap.get("majorName"));
            classVo.setCollege(stringStringMap.get("collegeName"));
            classVo.setRegisterTime(e.getRegisterTime());
            classVo.setId(e.getId());
            return classVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageUtils getClassPage(PageVo pageVo) {
        Page<SchoolClass> objectPage = new Page<SchoolClass>(pageVo.getPageNum(), pageVo.getPageSize());
        String collegeName = pageVo.getCollege();
        QueryWrapper<SchoolClass> userStudentQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(collegeName)) {
            SchoolCollege college = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("name", collegeName));
            userStudentQueryWrapper.eq("college_id",college.getId());
        }
        String select_major = pageVo.getMajor();
        if (!StringUtils.isEmpty(select_major)) {
            SchoolMajor major = schoolMajorService.getOne(new QueryWrapper<SchoolMajor>().eq("name", select_major));
            userStudentQueryWrapper.eq("major_id",major.getId());
        }
        String queryInfo = pageVo.getQuery();
        if (!StringUtils.isEmpty(queryInfo)) {
            userStudentQueryWrapper.and(wrapper -> {
                wrapper.like("name",queryInfo).or().eq("id",queryInfo);
            });
        }
        Page<SchoolClass> page = this.page(objectPage, userStudentQueryWrapper);
        List<SchoolClass> records = page.getRecords();
        List<ClassVo> collect = records.stream().map(e -> {
            Map<String, String> stringStringMap = userStudentService.packageCollegeAndMajor(e.getCollegeId(), e.getMajorId(), null);
            ClassVo allVo = new ClassVo();
            allVo.setId(e.getId());
            allVo.setRegisterTime(e.getRegisterTime());

            allVo.setCollege(stringStringMap.get("collegeName"));
            allVo.setMajor(stringStringMap.get("majorName"));
            allVo.setClassName(e.getName());
            return allVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public R addClass(ClassVo classVo) {
        SchoolClass schoolClass = new SchoolClass();
        SchoolCollege name = schoolCollegeService.getOne(new QueryWrapper<SchoolCollege>().eq("name", classVo.getCollege()));
        SchoolMajor name1 = schoolMajorService.getOne(new QueryWrapper<SchoolMajor>().eq("name", classVo.getMajor()));
        schoolClass.setCollegeId(name.getId());
        schoolClass.setMajorId(name1.getId());
        schoolClass.setRegisterTime(new Date());
        schoolClass.setName(classVo.getClassName());
        this.save(schoolClass);
        return R.ok();
    }

    @Override
    public ClassVo getBanJiByBjId(String banjiid) {
        SchoolClass schoolClass = this.getById(banjiid);
        Map<String, String> stringStringMap = userStudentService.packageCollegeAndMajor(schoolClass.getCollegeId(), schoolClass.getMajorId(), schoolClass.getId());
        ClassVo classVo = new ClassVo();
        classVo.setClassName(stringStringMap.get("className"));
        classVo.setMajor(stringStringMap.get("majorName"));
        classVo.setCollege(stringStringMap.get("collegeName"));
        classVo.setId(schoolClass.getId());
        classVo.setRegisterTime(schoolClass.getRegisterTime());


        return classVo;


    }

    @Override
    public void updateBanji(ClassVo classVo) {
        SchoolClass schoolClass = new SchoolClass();
        Map<String, Long> stringLongMap = userStudentService.releaseCollegeAndMajor(classVo.getCollege(), classVo.getMajor(),null);
        schoolClass.setName(classVo.getClassName());
        schoolClass.setRegisterTime(classVo.getRegisterTime());
        schoolClass.setCollegeId(stringLongMap.get("collegeId"));
        schoolClass.setMajorId(stringLongMap.get("majorId"));
        schoolClass.setId(classVo.getId());
        this.updateById(schoolClass);
    }


    @Transactional
    @Override
    public R deleteBanJi(Long id) {
        boolean flag = this.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error(101,"删除失败了，请联系管理员！");

    }
}




